//
//  UIView+Additions.m
//  HSCategoryKit
//
//  Created by huang on 2018/8/1.
//

#import "UIView+Addition.h"

@implementation UIView (Addition)

/**
 *  返回当前视图的控制器
 */
- (UIViewController *)currentViewController
{
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}

- (UIView *)roundCorner
{
    return [self roundCornersOnTopLeft:YES topRight:YES bottomLeft:YES bottomRight:YES radius:MIN(CGRectGetWidth(self.frame)*0.5, CGRectGetHeight(self.frame)*0.5)];
}

- (UIView *)roundCornerWithRadius:(float)radius
{
    return [self roundCornersOnTopLeft:YES topRight:YES bottomLeft:YES bottomRight:YES radius:radius];
}

- (UIView *)roundCornerWithRadius:(float)radius borderWidth:(float)borderWidth strokeColor:(UIColor *)strokeColor
{
    return [self roundCornersOnTopLeft:YES topRight:YES bottomLeft:YES bottomRight:YES radius:radius borderWidth:borderWidth strokeColor:strokeColor];
}

- (UIView *)roundCornersOnTopLeft:(BOOL)tl topRight:(BOOL)tr bottomLeft:(BOOL)bl bottomRight:(BOOL)br radius:(float)radius
{
    return [self roundCornersOnTopLeft:tl topRight:tr bottomLeft:bl bottomRight:br radius:radius borderWidth:0 strokeColor:[UIColor clearColor]];
}

- (UIView *)roundCornersOnTopLeft:(BOOL)tl topRight:(BOOL)tr bottomLeft:(BOOL)bl bottomRight:(BOOL)br radius:(float)radius borderWidth:(float)borderWidth strokeColor:(UIColor *)strokeColor
{
    self.clipsToBounds = YES;
    if (tl || tr || bl || br) {
        UIRectCorner corner = 0; 
        if (tl) {
            corner = UIRectCornerTopLeft;
        }
        if (tr) {
            UIRectCorner add = corner | UIRectCornerTopRight;
            corner = add;
        }
        if (bl) {
            UIRectCorner add = corner | UIRectCornerBottomLeft;
            corner = add;
        }
        if (br) {
            UIRectCorner add = corner | UIRectCornerBottomRight;
            corner = add;
        }
        
        UIView *roundedView = self;
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:roundedView.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)];
        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        maskLayer.frame = roundedView.bounds;
        maskLayer.path = maskPath.CGPath;
        roundedView.layer.mask = maskLayer;
        if (borderWidth > 0) {
            CGFloat halfWidth = borderWidth * 0.5;
            CGRect f = CGRectMake(halfWidth, halfWidth, CGRectGetWidth(roundedView.bounds) - borderWidth, CGRectGetHeight(roundedView.bounds) - borderWidth);
            UIBezierPath *borderPath = [UIBezierPath bezierPathWithRoundedRect:roundedView.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)];
            CAShapeLayer *borderLayer = [CAShapeLayer layer];
            borderLayer.path = borderPath.CGPath;
            borderLayer.fillColor = [UIColor clearColor].CGColor;
            borderLayer.strokeColor = strokeColor.CGColor;
            borderLayer.lineWidth = borderWidth;
            borderLayer.frame = CGRectMake(0, 0, CGRectGetWidth(f), CGRectGetHeight(f));
            [roundedView.layer addSublayer:borderLayer];
        }
        return roundedView;
    } else {
        return self;
    }
}

- (void)lineOnTop:(BOOL)top bottom:(BOOL)bottom left:(BOOL)left right:(BOOL)right strokeColor:(UIColor *)strokeColor
{
    CGFloat lineWidth = 0.5;
    CGFloat halfLineWidth = lineWidth * 0.5;
    if (strokeColor == nil) {
        strokeColor = [UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1.0];
    }
    CGFloat width = self.frame.size.width;
    CGFloat height = self.frame.size.height;
    if (top) {
        UIBezierPath *topLinePath = [UIBezierPath bezierPath];
        [topLinePath moveToPoint:CGPointMake(0, halfLineWidth)];
        [topLinePath addLineToPoint:CGPointMake(width, halfLineWidth)];
        CAShapeLayer *topLineLayer = [CAShapeLayer layer];
        topLineLayer.strokeColor = strokeColor.CGColor;
        topLineLayer.lineWidth = lineWidth;
        topLineLayer.path = topLinePath.CGPath;
        topLineLayer.fillColor = nil;
        [self.layer addSublayer:topLineLayer];
    }
    
    if (bottom) {
        UIBezierPath *bottomLinePath = [UIBezierPath bezierPath];
        [bottomLinePath moveToPoint:CGPointMake(0, height - halfLineWidth)];
        [bottomLinePath addLineToPoint:CGPointMake(width, height - halfLineWidth)];
        CAShapeLayer *bottomLineLayer = [CAShapeLayer layer];
        bottomLineLayer.strokeColor = strokeColor.CGColor;
        bottomLineLayer.lineWidth = lineWidth;
        bottomLineLayer.path = bottomLinePath.CGPath;
        bottomLineLayer.fillColor = nil;
        [self.layer addSublayer:bottomLineLayer];
    }
    
    if (left) {
        UIBezierPath *leftLinePath = [UIBezierPath bezierPath];
        [leftLinePath moveToPoint:CGPointMake(halfLineWidth, 0)];
        [leftLinePath addLineToPoint:CGPointMake(halfLineWidth, height)];
        CAShapeLayer *leftLineLayer = [CAShapeLayer layer];
        leftLineLayer.strokeColor = strokeColor.CGColor;
        leftLineLayer.lineWidth = lineWidth;
        leftLineLayer.path = leftLinePath.CGPath;
        leftLineLayer.fillColor = nil;
        [self.layer addSublayer:leftLineLayer];
    }
    
    if (right) {
        UIBezierPath *rightLinePath = [UIBezierPath bezierPath];
        [rightLinePath moveToPoint:CGPointMake(width - halfLineWidth, 0)];
        [rightLinePath addLineToPoint:CGPointMake(width - halfLineWidth, height)];
        CAShapeLayer *rightLineLayer = [CAShapeLayer layer];
        rightLineLayer.strokeColor = strokeColor.CGColor;
        rightLineLayer.lineWidth = lineWidth;
        rightLineLayer.path = rightLinePath.CGPath;
        rightLineLayer.fillColor = nil;
        [self.layer addSublayer:rightLineLayer];
    }
}


- (void)setX:(CGFloat)x {
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

- (void)setY:(CGFloat)y {
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

- (CGFloat)x {
    return self.frame.origin.x;
}

- (CGFloat)y {
    return self.frame.origin.y;
}

- (void)setCenterX:(CGFloat)centerX {
    
    CGPoint center = self.center;
    center.x = centerX;
    self.center = center;
    
}

- (CGFloat)centerX {
    return self.center.x;
}

- (void)setCenterY:(CGFloat)centerY{
    CGPoint center = self.center;
    
    center.y = centerY;
    
    self.center = center;
}

- (CGFloat)centerY {
    return self.center.y;
}

- (void)setWidth:(CGFloat)width {
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

- (void)setHeight:(CGFloat)height {
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

- (CGFloat)height {
    return self.frame.size.height;
}

- (CGFloat)width {
    return self.frame.size.width;
}

- (void)setSize:(CGSize)size {
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}

- (CGSize)size {
    return self.frame.size;
}

- (void)setOrigin:(CGPoint)origin {
    CGRect frame = self.frame;
    frame.origin = origin;
    self.frame = frame;
}

- (CGPoint)origin {
    return self.frame.origin;
}
- (CGFloat)top
{
    return self.frame.origin.y;
}

- (void)setTop:(CGFloat)top
{
    CGRect frame = self.frame;
    frame.origin.y = top;
    self.frame = frame;
}

- (CGFloat)left
{
    return self.frame.origin.x;
}

- (void)setLeft:(CGFloat)left
{
    CGRect frame = self.frame;
    frame.origin.x = left;
    self.frame = frame;
}


- (CGFloat)bottom
{
    return self.frame.size.height + self.frame.origin.y;
}

- (void)setBottom:(CGFloat)bottom
{
    CGRect frame = self.frame;
    frame.origin.y = bottom - frame.size.height;
    self.frame = frame;
}

- (CGFloat)right
{
    return self.frame.size.width + self.frame.origin.x;
}

- (void)setRight:(CGFloat)right
{
    CGRect frame = self.frame;
    frame.origin.x = right - frame.size.width;
    self.frame = frame;
}

@end
